import Vue from "vue"
import App from "./App.vue"
import router from "./router"
import "./plugins/vant.js"
import axios from "axios"
// 配置请求的基准URL地址
axios.defaults.baseURL = "http://112.74.99.5:3000/web/api"

//axios设置请求拦截器
axios.interceptors.request.use(
  config => {
    if (localStorage.getItem("token") && localStorage.getItem("id")) {
      config.headers.Authorization = "Bearer " + localStorage.getItem("token"); //设置响应头
    }
    return config
  },
  err => {
    console.log(err)
  }
);

//axios设置响应拦截器
axios.interceptors.response.use(
  response => {
    return response//拦截处理响应结果，直接返回需要的数据
  },
  err => {
    console.dir(err)
    if (err.response.status == 401 || 402) {
      router.push("/login")
    }
  }
);

Vue.config.productionTip = false;

//把axios注册到Vue
Vue.prototype.$http = axios;
new Vue({
  router,
  render: h => h(App)
}).$mount("#app")
